package com.atguigu.cloud.controller;

import com.atguigu.cloud.entities.Pay;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.service.PayService;
import com.atguigu.cloud.utils.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: huahua
 * @Date: 2024/5/29 0029
 * @Time: 18:46
 * @Description:
 */
@RestController
@RequestMapping("pay")
@Slf4j
@Tag(name = "支付模块",description = "支付CRUD")
public class PayController {
    @Resource
    private PayService payService;



    @PostMapping("add")
    @Operation(summary = "增")
    public ResultData<String> addPay(@RequestBody Pay pay){
        System.out.println(pay.toString());
        int add = payService.add(pay);
        return ResultData.success("成功插入记录" + add);
    }

    @DeleteMapping("del/{id}")
    @Operation(summary = "删")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id){
        int delete = payService.delete(id);
        return ResultData.success(delete);
    }

    @PutMapping("update")
    @Operation(summary = "改")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO){
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO,pay);


        int update = payService.update(pay);
        return ResultData.success("成功修改记录" + update);
    }

    @GetMapping("get/{id}")
    @Operation(summary = "查ID")
    public ResultData<Pay> getById(@PathVariable("id") Integer id){

        //暂停62秒
        try{TimeUnit.SECONDS.sleep(62);}catch (InterruptedException e){e.printStackTrace();}




        Pay byId = payService.getById(id);
        return ResultData.success(byId);
    }

    @GetMapping("all")
    @Operation(summary = "查全部")
    public ResultData<List<PayDTO>> getAll(){
        List<PayDTO> payDTOList = new ArrayList<>();
        List<Pay> all = payService.getAll();
        all.forEach(pay -> {
            PayDTO payDTO = new PayDTO();
            BeanUtils.copyProperties(pay,payDTO);
            payDTOList.add(payDTO);
        });

        return ResultData.success(payDTOList);
    }

    @GetMapping("error")
    public ResultData<Integer> getPayError(){
        throw new RuntimeException("调用时一定报错！");
    }


    @Value("${server.port}")
    private String port;

    @GetMapping("getInfo")
    public String getInfoByConsul(
            @Value("${atguigu.info}")
            String info
            ){
        return "Consul: " + info + " ServerPort:" + port;
    }
}
